package com.dongdongshop.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import com.dk.data.R;
import com.dk.util.PageResult;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.service.Tborderservice;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.print.Book;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderConroller {


    @DubboReference
    Tborderservice tds;


    //跳转页面
    @RequestMapping("tiao")
    public String tioa(){
        return "order";
    }




    //查询已支付的订单
    @RequestMapping("getStauts")
    @ResponseBody
    public Map<String, Object> getStauts(@RequestParam(defaultValue = "1") Integer pagenum,@RequestParam(defaultValue = "4") Integer pagesize,String date1,String date2){
        PageResult p = tds.getStauts(pagenum, pagesize,date1, date2);
        Map<String, Object> m=new HashMap<>();
        m.put("rows",p.getRows());
        m.put("total",p.getTotal());
        return  m;
    }






    //跳转页面
    @RequestMapping("getdatetiao")
    public String getdatetiao(String date1, String  date2,Model model){
        List<TbOrder> ls = tds.getDate(date1, date2);
        model.addAttribute("ls",ls);
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);
        return "getdate";
    }


    //查询日期范围内的
    @RequestMapping("getDate")
    @ResponseBody
    public R<List<TbOrder>> getDate(String date3, String  date4) throws Exception {
        //		1.创建WorkBook
        Workbook workbook = new XSSFWorkbook();
//		2.在Work里面创建一个sheet表
        Sheet sheet = workbook.createSheet();
//		4.创建表头cell
        Row row = sheet.createRow(0);//创建一个行对象
        String []title= {"日期","金额","已支付金额","待支付金额"};
        for(int i=0;i<title.length;i++) {
            Cell cell = row.createCell(i);//根据行创建出每一个单元格对象
            cell.setCellValue(title[i]);//给每个单元格赋值
        }
//		5.将查询结果放到表格中
        List<TbOrder> ls = tds.getDate(date3, date4);
        for (int i = 0; i <ls.size(); i++) {//遍历list集合
            Row row2 = sheet.createRow(i+1);//每一行
            //给每一列设置值
            Cell c1=row2.createCell(0);
            Date createTime = ls.get(i).getCreateTime();
            DateFormat format=new SimpleDateFormat("yyyy-MM-dd");//date转string
            String date1 = format.format(createTime);
            c1.setCellValue(date1);//第1列
            Cell c2=row2.createCell(1);
            c2.setCellValue(ls.get(i).getPayment().doubleValue());//第2列
            Cell c3=row2.createCell(2);
            c3.setCellValue(ls.get(i).getPayment().doubleValue());//第3列
            Cell c4=row2.createCell(3);
            c4.setCellValue(ls.get(i).getPostFee());//第4列
        }
//		6.将workbook通过io写到某个文件下
        FileOutputStream fos=new FileOutputStream("D:/e/book/a"+".xlsx");
        workbook.write(fos);
        fos.close();



        return R.ok();
    }



}
